<template>
  <div class="module">
    <div class="moduleList" @click.stop="closeMask = false"
      :style="{ pointerEvents: closeMask ? 'auto' : 'none', opacity: closeMask ? 1 : 0 }">
      <div @click.stop="closeMask = true" class="rightCommon" :style="{ right: closeMask ? '0px' : '-450px' }">
        <div class="moduleStyle" v-for="module, index in routerList" :key="index">
          <div class="moduleLabel">
            {{ module.meta.title }}
          </div>
          <div class="common">
            <div @click.stop="addModule(item)" class="item"
              v-show="item.meta.breedType.includes(breedTypeObj[breedType])" v-for="item in module['children']"
              :key="item.path">
              <div class="moduleIcon">
                <!-- <i class="el-icon-folder-add"></i> -->
                <svg-icon class="sveIcon" v-if="item.meta.icon" :icon-class="item.meta.icon" />
                <i v-else class="el-icon-folder-add"></i>
              </div>
              <div class="name">{{ item.meta.title }}</div>
              <div class="delModule" v-show="isChoose(item.path, item)">
                <i class="el-icon-success"></i>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="content" id="scrollContent">
      <div class="earlyModule">
        <div class="title titleRow">
          <div class="left">
            <div class="point"></div> <span>常用功能</span>
          </div>
          <div class="right">
            <el-button type="primary" @click="colseModule" icon="el-icon-menu">管理常用功能</el-button>
            <el-button type="primary" @click="openNotify">更改布局</el-button>
          </div>
        </div>
        <div class="table common">
          <draggable v-model="earlyModuleList" class="table common" animation="300" @end="onDragEnd">
            <div class="item" v-for="item, index in earlyModuleList" :key="index" @click="jumpPage(item)">
              <div class="moduleIcon">
                <svg-icon class="sveIcon" v-if="item.icon" :icon-class="item.icon" />
                <i v-else class="el-icon-folder-add"></i>
              </div>
              <div class="name">{{ item.menuName }}</div>
            </div>
            <div class="item noMenu" @click="closeMask = true">
              <div class="icon">
                <i class="el-icon-plus"></i>
              </div>
              <div class="name">添加常用功能</div>
            </div>
          </draggable>
        </div>
      </div>
      <div class="earlyModule">
        <div class="title">
          <div class="point"></div> <span>代办事项</span>
        </div>
        <div class="table">
          <Agency></Agency>
        </div>
      </div>
      <draggable v-model="componendList" class="warningModule" ref="targetElement" @end="onDragEndModule">
        <div class="earlyModule" v-for="item, index in componendList" :key="index">
          <div class="title">
            <div class="point"></div> <span>{{ item.moduleName }}</span>
          </div>
          <div class="table">
            <OverdueAgency v-if="item.comName == 'OverdueAgency'"></OverdueAgency>
            <EarlyWarning v-if="item.comName == 'EarlyWarning'" :type="item.type"></EarlyWarning>
          </div>
        </div>
        <!-- <div class="earlyModule">
                        <div class="title">
                            <div class="point"></div> <span>消杀提醒</span>
                        </div>
                        <div class="table">
                            <EarlyWarning :warningType="'DISINFECT'"></EarlyWarning>
                        </div>
                    </div> -->
      </draggable>
    </div>
  </div>
</template>
<script>
import MainSlot from "@/components/MainSlot";
import earlyWarningCenter from "@/api/earlyWarningCenter";
import Agency from './components/agency';//代办
import OverdueAgency from './components/overdueAgency';//逾期代办
import EarlyWarning from './components/earlyWarning';//预警
import variables from "@/styles/variables.scss";



import draggable from 'vuedraggable';

export default {
  components: { MainSlot, draggable, Agency, OverdueAgency, EarlyWarning },
  data() {
    return {
      closeMask: false,
      tableData1: [],
      tableDataDB: [],
      totalDB: 100,
      componendList: [
        { id: 1, moduleName: '代办逾期', comName: 'OverdueAgency', type: '' },
        { id: 2, moduleName: '消杀预警', comName: 'EarlyWarning', type: 'DISINFECT' },
        { id: 3, moduleName: '饲喂预警', comName: 'EarlyWarning', type: 'FEED' },
        { id: 4, moduleName: '产出预警', comName: 'EarlyWarning', type: 'OUTPUT' },
        { id: 5, moduleName: '预防预警', comName: 'EarlyWarning', type: 'PREVENT' },
        { id: 6, moduleName: '过程预警', comName: 'EarlyWarning', type: 'PROCESS' },
        { id: 7, moduleName: '密度过高提醒', comName: 'EarlyWarning', type: 'MORTALITY' },
        { id: 8, moduleName: '用药预警配置', comName: 'EarlyWarning', type: 'MEDICINE' },
        { id: 9, moduleName: '水产死亡过高提醒', comName: 'EarlyWarning', type: 'DENSITY' }
      ],
      earlyModuleList: [],
      moduleList: [],
      routerList: [],
      breedType: sessionStorage.breedType,
      userType: '',
      breedTypeObj: {
        "HANGING_WATER_BREED": "DSYZ",
        "TRADITION_BREED": "CTYZ",
        "CIRCULATE_BREED": "XHSYZ",
        "RUNWAY_BREED": "PDYZ"
      },
    }
  },
  mounted() {
    this.getRouter();
    this.queryByLoginUser();
    this.queryModuleList();
    console.log('variablesworkbench',variables);
  },
  methods: {
    colseModule() {
      this.closeMask = true;
      this.$store.commit('settings/setMenuFlag', 'close');
    },
    async onDragEnd() {
      let idList = this.earlyModuleList.map(item => item.userFrequentlyUsedMenuId)
      await earlyWarningCenter.saveSort(idList)
      this.queryByLoginUser();
    },
    async onDragEndModule() {
      let idList = this.componendList.map(item => item.id)
      console.log(idList)
      await earlyWarningCenter.saveSortModule(idList)
      this.queryModuleList();
    },
    queryModuleList() {
      earlyWarningCenter.queryModuleList().then(res => {
        if (res.code == 1) {
          let dataList = res.data || [];
          if (dataList.length == this.componendList.length) {
            let order = dataList.map(item => item.menuName);
            this.componendList.sort((a, b) => {
              const aIndex = order.findIndex(id => id == a.id);
              const bIndex = order.findIndex(id => id == b.id);
              return aIndex - bIndex;
            });
          }
        }
      })
    },
    getRouter() {
      let routers = this.$router.options.routes;
      console.log(routers)
      let filterRouter = ['*', "/404", "/login", "/earlyWarningCenter"];
      let routerList = [];
      routerList = routers.filter(item => {
        let systemFlag = item.meta?.systemFlag
        if (!filterRouter.includes(item.path) && systemFlag == 'SC') {
          return true
        } else {
          return false
        }
      })
      this.routerList = routerList
      console.log(routerList)
    },
    // getRouterlistNAme(searchPath) {
    //   let list = this.$router.options.routes
    //   if (searchPath) {
    //     list = this.$router.options.routes.filter(item => {
    //       return item.path == searchPath
    //     })[0].children
    //   }
    //   list.forEach(item => {
    //       if (item.meta && item.meta.title) {
    //         console.log(item.path, ' ', item.meta.title)
    //       }
    //     })
    // },
    isChoose(path, item) {
      let row = this.earlyModuleList.find(obj => obj.router == path);
      if (row) {
        item.id = row.userFrequentlyUsedMenuId;
        return true
      } else {
        item.id = '';
        return false
      }
    },
    async delById(id) {
      await earlyWarningCenter.delById(id);
      this.queryByLoginUser();
    },
    addModule(row) {
      if (row.id) {
        this.delById(row.id)
      } else {
        let params = {
          menuName: row.meta.title,
          router: row.path,
          icon: row.meta.icon
        }
        earlyWarningCenter.add({ ...params }).then(res => {
          if (res.code == 1) {
            // this.$message.success('添加成功')
          } else {
            this.$message.warning(res.msg || '添加失败')
          };
          this.queryByLoginUser();
        }).catch(err => {
          console.log(err)
        })
      }
    },
    queryByLoginUser() {
      earlyWarningCenter.queryByLoginUser({}).then(res => {
        if (res.code == 1) {
          this.earlyModuleList = res.data;
        } else {
          this.earlyModuleList = [];
        };
      }).catch(err => {
        console.log(err);
      })
    },
    jumpPage(row) {
      this.$router.push({ path: row.router });
    },
    openNotify() {
      this.$notify({
        title: '提示',
        message: this.$createElement('i', { style: 'color: teal' }, '您可以拖动提醒表单切换位置展示！')
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.module {
  height: 100%;
  background-color: #ffffff00;
  padding: 0;
  margin: 10px;
  overflow-y: auto;
}

.content {
  width: 100%;
  height: 100%;
  padding: 5px;
  overflow-y: auto;
  background-color: #ffffff00;

  .earlyModule {
    width: 100%;
    background-color: #ffffff;
    box-shadow: 0 6px 10px 0px rgba(0, 21, 41, 0.05);
    margin-bottom: 15px;
    border-radius: 6px;


    .title {
      line-height: 50px;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      background-color: #e0e8ee;
      color: #4780b0;
      border-radius: 6px 6px 0 0;
      padding: 0 20px;

      .left {
        display: flex;
        justify-content: flex-start;
        align-items: center;
      }

      .point {
        width: 10px;
        height: 10px;
        background-color: #4780b0;
        margin-right: 5px;

      }
    }

    .titleRow {
      justify-content: space-between;
    }

    .table {
      padding: 15px;
    }
  }

  .warningModule {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;

    .earlyModule {
      width: calc(50% - 8px);
      transition: all 0.3s;
      border: 1px solid #375e7e48;

      &:nth-child(odd) {
        margin-right: 16px;
      }
    }

  }

  .paginationModule {
    text-align: right;
    margin-top: 5px;
  }
}

.common {
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
  align-content: flex-start;
  min-height: 115px;

  .item {
    width: 95px;
    margin: 10px 15px;
    transition: all 0.3s;
    position: relative;
    cursor: pointer;

    &:hover {
      transform: scale(1.1)
    }

    .moduleIcon {
      width: 60px;
      height: 60px;
      background: linear-gradient(0deg, #62b6fa, #2184d4);
      border-radius: 10px;
      margin: 0 auto;
      text-align: center;
      line-height: 60px;
      color: #ffffff;
      font-size: 18px;
    }

    .name {
      margin-top: 10px;
      color: #666666;
      width: 100px;
      text-align: center;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      -o-text-overflow: ellipsis;
      font-size: 12px;
    }
  }


}

.noMenu {
  .icon {
    width: 60px;
    height: 60px;
    background: linear-gradient(0deg, #e4e4e4, #d8d8d8);
    border-radius: 10px;
    margin: 0 auto;
    text-align: center;
    line-height: 60px;
    color: #ffffff;
    font-size: 18px;
    border: 1px solid #cecece;
  }
}

.rightCommon {
  position: fixed;
  right: 0;
  top: 0;
  width: 440px;
  height: 100%;
  background-color: #ffffff;
  box-shadow: 0 6px 10px 0px rgba(0, 21, 41, 0.05);
  opacity: 1;
  transition: all 0.2s;
  padding: 15px 0;
  overflow-y: auto;

  .moduleLabel {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    font-size: 16px;
    font-weight: bold;
    padding-left: 15px;
  }

  .moduleStyle {
    width: 100%;
    padding: 15px;
    margin-bottom: 30px;
  }

  .item {
    width: calc(100% / 3 - 40px);
    margin: 20px 20px;
  }
}

.moduleList {
  position: fixed;
  right: 0;
  top: 0;
  width: 100%;
  height: 100vh;
  z-index: 9;
  background-color: #00000027;
  transition: all 0.2s;
}

.delModule {
  color: rgb(3, 170, 3);
  font-size: 18px;
  position: absolute;
  top: -10px;
  right: 0;
}

.sveIcon {
  font-size: 24px;
  filter: brightness(100);
}
</style>